package com.travel.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

@Service
public class MailUtil {
    @Autowired
    private JavaMailSender javaMailSender;

    public boolean sendEmailCode(String code,String to) {
        try {
            // 用来设置邮件信息
            SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
            // 设置邮件标题
            simpleMailMessage.setSubject("登录验证码");
            // 设置邮件内容
            simpleMailMessage.setText("尊敬的用户，您好！\n\n" +
                    "欢迎使用趣享旅程！\n\n" +
                    "您的验证码是：" + code + "\n\n" +
                    "请在 5 分钟内完成验证。如非本人操作，请忽略此邮件。\n\n"
            );
            // 设置源邮箱
            simpleMailMessage.setFrom("2601803754@qq.com");
            // 设置目的邮箱
            simpleMailMessage.setTo(to);
            // 发送
            javaMailSender.send(simpleMailMessage);
            // 没有出现异常，正常发送，返回true
            return true;
        } catch (MailException e) {
            // 发送过程中，发生错误，打印错误信息，返回false
            e.printStackTrace();
            return false;
        }
    }
}
